package org.scrumfx.service;

import java.util.List;

import org.scrumfx.bean.Project;
import org.scrumfx.business.ProjectBusiness;
import org.scrumfx.exception.NullRequestException;
import org.scrumfx.message.request.project.DeleteProjectRequest;
import org.scrumfx.message.request.project.GetProjectRequest;
import org.scrumfx.message.request.project.SaveProjectRequest;
import org.scrumfx.message.request.project.SearchProjectRequest;
import org.scrumfx.message.response.project.DeleteProjectResponse;
import org.scrumfx.message.response.project.GetProjectResponse;
import org.scrumfx.message.response.project.SaveProjectResponse;
import org.scrumfx.message.response.project.SearchProjectResponse;


public class ProjectService implements IProjectService
{

  @Override
  public GetProjectResponse getProject(GetProjectRequest request)
  {
    GetProjectResponse response = new GetProjectResponse();

    if (request == null)
    {
      response.exception = new NullRequestException();
    }
    else
    {
      Project project = projectBusiness.getProject(request.projectId);
      // TODO convert Project into ProjectDetail
    }
    return response;
  }

  @Override
  public SearchProjectResponse searchProjects(SearchProjectRequest request)
  {
    SearchProjectResponse response = new SearchProjectResponse();

    if (request == null)
    {
      response.exception = new NullRequestException();
    }
    else
    {
      List<Project> projects = projectBusiness.searchProject(request.criterias);
      // TODO convert List<Project>  into List<ProjectSummary> 
    }
    return response;
  }

  @Override
  public SaveProjectResponse saveProject(SaveProjectRequest request)
  {
    SaveProjectResponse response = new SaveProjectResponse();

    if (request == null)
    {
      response.exception = new NullRequestException();
    }
    else
    {
      // TODO convert ProjectDetail into Project
      Project project = null;
      project = projectBusiness.saveProject(project);
      // TODO convert Project into ProjectDetail
    }
    return response;
  }

  @Override
  public DeleteProjectResponse deleteProject(DeleteProjectRequest request)
  {
    DeleteProjectResponse response = new DeleteProjectResponse();

    if (request == null)
    {
      response.exception = new NullRequestException();
    }
    else
    {
      projectBusiness.deleteProject(request.projectId);
    }
    return response;
  }

  /**********************/
  /** Spring injection **/
  /**********************/

  private ProjectBusiness projectBusiness;

  public ProjectBusiness getProjectBusiness()
  {
    return projectBusiness;
  }

  public void setProjectBusiness(ProjectBusiness projectBusiness)
  {
    this.projectBusiness = projectBusiness;
  }

}
